//
//  ViewController.m
//  27-HomeWork调研AVPlayer并完成播放功能
//
//  Created by dllo on 16/8/20.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "PlayView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet PlayView *playView;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, assign) CGFloat sumPlayOperation;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGFloat currentTime;
@property (nonatomic, assign) float allTime;
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

@end
//http://m6.file.xiami.com/874/70874/2081396310/1772239439_11360275_h.mp3
//http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1"]];
    
    [self createAVPlayer];
    [self play_pauseButton];
    [self next_lastButton];
    [self kvo];
    
    
}
- (void)createAVPlayer {
    //设置播放的url
    NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
    NSURL *url = [NSURL URLWithString:playString];
    //设置播放的项目
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    //初始化player对象
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
    //设置播放页面
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    //设置播放窗口和当前视图之间的比例显示内容
    layer.videoGravity = AVLayerVideoGravityResizeAspect;
    //添加播放视图到self.view
    [self.view.layer addSublayer:layer];
    //设置播放进度的默认值
    self.slider.value = 0.0;
    self.slider.minimumValue = 0.0;
    self.slider.maximumValue = 1.0;
    //设置播放的默认音量值
    self.player.volume = 1.0f;
    [self alltimeLabel];
}

- (void)kvo {
    __weak ViewController *vc = self;
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        vc.currentTime = CMTimeGetSeconds(time);
    }];
}
- (void)play_pauseButton {
    [self.playView play:^(Button_image *play) {
        [self.player play];
        self.timer.fireDate = [NSDate distantPast];// 恢复timer

    } pause:^(Button_image *pause) {
        [self.player pause];
        self.timer.fireDate = [NSDate distantFuture]; // 暂停timer

    }];
}
- (void)next_lastButton {
    [self.playView last:^(Button_image *last) {
        
    } next:^(Button_image *next) {
        
    }];
}
- (IBAction)changeProgress:(id)sender {
    self.sumPlayOperation = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
    //CMTimeMake(a,b) a表示当前时间，b表示每秒钟有多少帧
    [self.player seekToTime:CMTimeMakeWithSeconds(self.slider.value * self.sumPlayOperation, self.player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
    }];
}
#pragma mark - 定时器
-(NSTimer *)timer {
    if (!_timer)
    { _timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:true];
    }
    return _timer;
}
// 更新当前进度
- (void)updateProgress {
    self.allTime = (CGFloat)self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
   
    float progress = self.currentTime / _allTime;
    self.slider.value = progress;
    [self currentTimeLabel];
}
- (void)alltimeLabel {

    NSString *rightString = [NSString stringWithFormat:@"%02d:%02d", (int)self.allTime / 60, (int )self.allTime % 60];
    self.rightLabel.text = rightString;
    [self currentTimeLabel];
}
- (void)currentTimeLabel {
    NSString *legtString = [NSString stringWithFormat:@"%02d:%02d", (int)self.currentTime / 60, (int )self.currentTime % 60];
    self.leftLabel.text = legtString;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
